import React from 'react';
import Taro from '@tarojs/taro';
import { View, Text, Image } from '@tarojs/components';

import { AtButton, AtCurtain } from 'taro-ui';

import login from './login.png';
import ZbSeparator from '../common/Separator';
import Tips from '@/utils/tips';

interface IZbLoginProps {
  isShow: boolean;
  onClose: () => void;
  authStore: STORE.authStoreProps;
}

const ZbLogin = (props: IZbLoginProps) => {
  const { isShow, onClose, authStore } = props;

  // const [isLogin, setIsLogin] = React.useState(false);

  React.useEffect(() => {
    // setIsLogin(isShow);
    console.log('authStore', authStore, isShow);
  }, []);

  const getUserInfo = async (e: any) => {
    console.log(e);
    if (e.detail.userInfo) {
      //同意
      let res = await Taro.login();
      //将code存到数据库
      //登录后台
      let param = e.detail.userInfo;
      param.code = res.code;
      const user = await authStore.wxLogin(param);
      if (user) {
        onClose();
      }
    } else {
      await Tips.toast('授权失败，请重新授权');
    }
  };

  return (
    <AtCurtain
      isOpened={isShow}
      onClose={() => {
        onClose();
      }}
    >
      <View className="rounded-lg" style="background:#eeeeee">
        <View className="p-2 text-center">
          <Text className="text-xl font-black">需要您的授权登录</Text>
        </View>
        <ZbSeparator height={1} bgColor="#F5F3FF" />
        <View className="pd20">
          <View className="text-center">
            <Text className="text-lg text-red-500">为了提供更好的服务</Text>
          </View>
          <View className="text-center">
            <Text className="text-lg text-red-500">请在稍后的提示框中点击“允许”</Text>
          </View>
          <View>
            <Image className="w-full" mode="widthFix" src={login} />
          </View>
        </View>
        <View className="p-2">
          <AtButton className="rounded-full py-3 px-6" type="primary" openType="getUserInfo" onGetUserInfo={getUserInfo}>
            我知道了
          </AtButton>
        </View>
      </View>
    </AtCurtain>
  );
};

export default ZbLogin;
